import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import food from "./hero";
import { Observable, throwError } from 'rxjs';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit{
  constructor(
    private http: HttpClient
  ) {  }
  ngOnInit(): void {
  }
  element=HTMLElement;
  foo = new food();
  name: string = '';
  roomid: string = '';
  total:object ={};
  pho:any
  inchange(e: any) {                 //保存图片信息
    this.pho=e.target.files[0];
  };
  buttonchange() {       //传name,room,base64
   this.total={name:this.name,authority:this.roomid};
   var reader = new FileReader();
   if(this.name=='' || this.roomid==''|| this.pho==null)
   {
     alert("填写信息不全");
   }
   else{
    reader.readAsDataURL(this.pho);
    reader.onload  = e => {
     if(reader.result==null)
     {
       return
     };
     if(typeof(reader.result)=="string")
     {
       this.total={
         name:this.name,
         authority:this.roomid,
         photoBase64:reader.result
       };
       console.log(this.total)
       this.addHero(this.total).subscribe(data=>console.log(data));
     };
   }
  };
  };
 formclick() {
  this.foo.inclick();
  }
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
    })
  };
  postregisterUrl = 'http://113.55.52.87:8080/register';
  // postregisterUrl = 'localhost:8080/register';
  addHero(tal:object): Observable<object> {
    return this.http.post<object>(this.postregisterUrl,tal,this.httpOptions)
  };
 }
